#!/bin/bash

#./deploy-api.sh

name="scms"
version="v1.0.0"
portMapping="9000:9000"
envFile="/opt/build/env.list"
dockerFile="./cicd/Dockerfile"

echo "---------------------------------------------------------------------------------------------------------------------------------------------"

# 构建 java项目
mvn clean
mvn install

echo "Deploy ${name}:${version}"

echo "Name              = ${name}"
echo "Version           = ${version}"
echo "PortMapping       = ${portMapping}"
echo "EnvFile       	= ${envFile}"

echo "step 1 : shoutdown and remove container"
docker ps -a --filter "name=$name" | awk '{print $1}'| while read cid
do
  if [ $cid != 'CONTAINER' ];then
    echo "docker rm -f $cid"
    docker rm -f $cid
  fi
done

echo "step 2 : remove image"
echo "docker rmi ${name}:${version}"
docker rmi ${name}:${version}

echo "step 3 : build new image"
echo "docker build -t ${name}:${version} -f ${dockerFile} ."
docker build -t ${name}:${version} -f ${dockerFile} .

echo "step 4 : run container"
echo "docker run --restart=always -d -p ${portMapping} --env-file ${envFile} --name ${name} ${name}:${version}"
docker run --restart=always -d -p ${portMapping} --env-file ${envFile} --name ${name} ${name}:${version}

echo "step 5 : check deploy"
echo "docker images"
docker images
echo "docker ps -a"
docker ps -a

echo "${name}:${version} deploy over!"